//@charset "utf-8";
//选股 - 数据
define(function (require, exports, module) {
	var ClientBaseModel = require('./clientbase');
	var clientQuote = require('../lib/clientquote');
	var AjaxBaseModel = require('./ajaxbase');
	var ajaxBaseModel = new AjaxBaseModel();

	var thsUtil = null;
	try {
		thsUtil = external.createObject('Util');
	} catch(e) {
		//alert(e.message);
	}
	var StockSearchModel = ClientBaseModel.extend({
		getStockListStr : function (tag) {
			var result = this.get('result');
			var codeList = [];
			codeList.push(result['code']['code']);
			$.each(result.codelist, function (i, item) {
				codeList.push(item['code']);
			});

			return codeList.join(tag);
		},
		getStockList : function (stockCode, callback) {
			var self = this;
			if (null === thsUtil) {
				self.set('result', {
					'block' : {
						'code' : '--',
						'zqmc' : '--'
					},
					'code' : {
						'code' : stockCode,
						'zqmc' : '--',
						'zhangdiefu' : '--',
						'new' : '--'
					},
					'codelist' : [],
					'total' : 0
				});

				callback();
			}
			var blockList = thsUtil.getStockConceptBlock(stockCode);

			var param = {
				code : blockList,
				type : 'new,zhangdiefu,zqmc'
			};

			var quoteCallback = function (res) {
				var maxZhangdiefu = null;
				var maxZhangdiefuBlock = null;

				for (var i in res) {
					if (null === maxZhangdiefuBlock) {
						maxZhangdiefu = res[i]['zhangdiefu'];
						maxZhangdiefuBlock = i;
						continue;
					}
					if (res[i]['zhangdiefu'] > maxZhangdiefu) {
						maxZhangdiefu = res[i]['zhangdiefu'];
						maxZhangdiefuBlock = i;
					}
				}

				var allCodeList = thsUtil.getBlockStockByCode(maxZhangdiefuBlock);
				allCodeList = allCodeList.split(',');
				var needCodeList = [];
				for (var j in allCodeList) {
					if (stockCode === allCodeList[j]) {
						continue;
					}
					needCodeList.push({
						'code' : allCodeList[j],
						'zqmc' : '--',
						'zhangdiefu' : '--',
						'new' : '--'
					});
				}

				self.set('result', {
					'block' : {
						'code' : maxZhangdiefuBlock,
						'zqmc' : res[maxZhangdiefuBlock]['zqmc']
					},
					'code' : {
						'code' : stockCode,
						'zqmc' : '--',
						'zhangdiefu' : '--',
						'new' : '--'
					},
					'codelist' : needCodeList,
					'total' : needCodeList.length
				});

				self.fixQuoteData(allCodeList, callback);
			};

			clientQuote.getStockMinData(param, quoteCallback);
		},
		fixQuoteData : function (allCodeList, callback) {
			var self = this;
			var result = self.get('result');
			if (undefined === result.codelist || result.codelist.length <= 0) {
				callback();
				return ;
			}

			var param = {
				code : allCodeList.join(','),
				type : 'zqmc,new,zhangdiefu'
			};
			var quoteCallback = function (res) {
				try {
					result.code['zqmc'] = undefined !== res[result.code['code']]['zqmc'] ? 
						res[result.code['code']]['zqmc'] : '--';
					result.code['zhangdiefu'] = undefined !== res[result.code['code']]['zhangdiefu'] ? 
						res[result.code['code']]['zhangdiefu'] : '--';
					result.code['new'] = undefined !== res[result.code['code']]['new'] ? 
						res[result.code['code']]['new'] : '--';

					for (var j in result.codelist) {
						var sortZhangdiefu = '--';
						if (
							undefined === res[result.codelist[j]['code']]['zhangdiefu'] ||
							isNaN(res[result.codelist[j]['code']]['zhangdiefu'])
						) {
							sortZhangdiefu = '--';
						} else {
							sortZhangdiefu = parseFloat(res[result.codelist[j]['code']]['zhangdiefu']);
						}
						var newPrice = undefined !== res[result.codelist[j]['code']]['new'] ? res[result.codelist[j]['code']]['new'] : '--';
						var zhangdiefu = undefined !== res[result.codelist[j]['code']]['zhangdiefu'] ? res[result.codelist[j]['code']]['zhangdiefu'] : '--';
						result.codelist[j]['zqmc'] = undefined !== res[result.codelist[j]['code']]['zqmc'] ? 
							res[result.codelist[j]['code']]['zqmc'] : '--';
						result.codelist[j]['zhangdiefu'] = zhangdiefu == '--' ? '--' : 	parseFloat(zhangdiefu).toFixed(2);
						result.codelist[j]['newprice'] = newPrice == '--' ? '--' : parseFloat(parseFloat(newPrice).toFixed(2));
						result.codelist[j]['sortzdf'] = sortZhangdiefu;
					}
					
					result.codelist = result.codelist.sort(self.sortTableFun('sortzdf', 'desc'));
					self.set('result', result);
				} catch (e) {
					callback();
					return ;
				}
				
				callback();
			};
			clientQuote.getStockMinData(param, quoteCallback);
		},
		sortData : function(sortBy, sortWay, list, page) {
			return ajaxBaseModel.sortData(sortBy, sortWay, list, page);
		}
	});

	module.exports = new StockSearchModel();
});
